#pragma once

#ifndef _HUNTERHTTP_H
#define _HUNTERHTTP_H

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>

#include "hunterAPI.hpp"

using namespace std;

string path_home = "http://botanstvo/club";
string div_error = "border:4px; border-style:solid; border-color:#ff0000;align:center;",
	   menu = ""
	   ;
	   
void o_header(string title)
{
	cout << "<html> <head> <title> " << title << "</title><link rel=\"shortcut icon\" href=\"" << path_home << "/cgi-bin/img/22.png\">";
	cout <<	"<style>tr {text-align:center;} tr.alt td {text-align:center;color:#000000;background-color:#e1ddcf;}</style>";
	cout <<"</head><body>";
//	cout << "<script> var stateObj = { foo: \"blah\" };history.pushState(stateObj, \"page 2\", \"" << path_home << "\"); </script>";
}
void o_footer(void)
{
	cout << "</body></html>";
}
void o_login_error(string emsg)
{
	cout << "<div style = '" << div_error << "'> <p> Login failed! <br> This may be caused by several reasons. <br> Emsg is: <br> <i> " << emsg << "</i> </p> <a href = '" << path_home << "'> Return </a> </div>";
}
void o_internal_error(string emsg)
{
	cout << "<div style = '" << div_error << "'> <p> Internal error<br> This may be caused by several reasons. <br> Emsg is: <i>" << emsg << "</i></p> <a href = '" << path_home << "'> Return </a> </div>";
}
void o_menu(string ustat)
{
	cout << "<table cellpadding = 20 style = '" << menu << "'><tr><td><a href = '" << path_home << "/cgi-bin/blog.html'><img style = 'width:64px;height:64px;' src = '"<< path_home << "/cgi-bin/img/03.png'><h1>BLOG</h1></a></td>";
	if (find(ustat.begin(), ustat.end(), 'a') != ustat.end())
		cout << "<td><a href = '" << path_home << "/cgi-bin/submit.html'><img style = 'width:64px;height:64px;' src = '" << path_home << "/cgi-bin/img/20.png'><h1>SUBMIT</h1></a></td>";
	cout << "<td><a href = '" << path_home << "/cgi-bin/stats.html'><img style = 'width:64px;height:64px;' src = '" << path_home << "/cgi-bin/img/04.png'><h1>STATS</h1></a><td>";
	cout << "<td><a href = '" << path_home << "/cgi-bin/faq.html'><img style = 'width:64px;height:64px;' src = '" << path_home << "/cgi-bin/img/25.png'><h1>FAQ</h1></a></td>";
	if (find(ustat.begin(), ustat.end(), 'Q') != ustat.end())
		cout << "<td><a href = '" << path_home << "/cgi-bin/alchemy.html'><img style = 'width:64px;height:64px;' src = '" << path_home << "/cgi-bin/img/22.png'><h1>ALCHEMY</h1></a></td>";
	cout << "</tr></table>";
}
void o_player(HunterAPI::Player &pl)
{
	cout << "<table><tr><td rowspan = 2><img style = 'width:64px;height:64px;' src = '" << path_home << "/cgi-bin/img/27.png'></td><td><a href = \"" << path_home << "/cgi-bin/player.cgi?query=" << pl.id << "\"><h3>" << pl.username << "</h3></a></td></tr><tr><td>Rating: <span style = 'color:green;font-weight:bold;'>" << pl.rating << "</span><br>Engaged: " << ((pl.engaged > 0) ? "Yes" : "No") << "<br><a href = \"" << path_home << "\">Logout</a></td></tr></table>";
}

void o_upper_visible(HunterAPI::Player &pl)
{
	cout << "<table style = \"width:100%;\"> <tr> <td align = \"left\">";
	o_menu(pl.stats);	
	cout << "</td><td align = \"right\">";
	cout << fixed << setprecision(1);
	o_player(pl);
	cout << "</td></tr></table>";
}

void o_blog_entry_header()
{
	cout << "<center><table>";
}
void o_blog_entry(HunterAPI::BlogEntry &be, int i, HunterAPI::HiveMind &hm)
{
	HunterAPI::Player p;
	hm.getPlayerById(be.author_id, p);
	cout << "<script type = \"text/javascript\">function show_blog_" << i << "() { if (document.getElementById(\"div" << i << "\").style.visibility == \"visible\") {document.getElementById(\"div" << i << "\").style.visibility = \"hidden\"; document.getElementById(\"div" << i << "\").style.height = \"0px\"} else { document.getElementById(\"div" << i << "\").style.visibility = \"visible\"; document.getElementById(\"div" << i << "\").style.height = \"\"} } </script>";
	cout << "<tr" << ((i % 2) ? " class = \"alt\"" : "") << "><td style = \"width:50px;\">" << be.date << "</td><td><img src = \"" << path_home << "/cgi-bin/img/03.png\" style = \"width:16px;height:16px;\">" << p.username << "</td><td style = \"width:800px;\">" << be.header << "</td><td><button onClick = \"javascript:show_blog_" << i << "()\">View/Hide</button></td></tr><tr><td colspan = 3><div id = \"div" << i << "\" style = \"visibility:hidden;height:0px;text-align:center;\">" << be.text << "</div></td></tr>";
}
void o_blog_entry_footer()
{
	cout << "</table></center>";
}

void o_stat_player_header()
{
	cout << "<center><table id = \"statpl\">";
}
void o_stat_player(HunterAPI::Player &p, int i, HunterAPI::HiveMind &hm)
{
	cout << fixed << setprecision(1);
	cout << "<tr" << ((i % 2) ? " class = \"alt\"" : "") << "><td style = \"width:30px;\">" << i + 1 << "</td><td><img style = \"width:16px;height:16px;\" src = \"" << path_home << "/cgi-bin" << ((find(p.stats.begin(), p.stats.end(), 'a') == p.stats.end()) ? "/img/17.png" : "/img/22.png") << "\">" << ((p.engaged > 0) ? "<img style = \"width:16px;height:16px;\" src = \"" + path_home + "/cgi-bin/img/05.png\">" : "") << "</td><td style = \"width:300px;\"><a href = \""<< path_home << "/cgi-bin/player.cgi?query=" << p.id << "\">" << p.username << "</a></td><td style = \"width:50px;\">";
	if (i == 0)
		cout << "<span style = \"font-height:14px;font-weight:bold;color:green;\">";
	cout << p.rating;
	if (i == 0)
		cout << "</span>";
	cout << "</td></tr>";
}
void o_stat_player_footer()
{
	cout << "</table></center>";
}

string getCookie(void)
{
	return HunterAPI::parseHTTPinput(string(getenv("HTTP_COOKIE")))["name"];
}

string getQuery(void)
{
	return HunterAPI::parseHTTPinput(string(getenv("QUERY_STRING")))["query"];
}

#endif
